import { Component, OnInit } from '@angular/core';
import { HttpService } from '../../service/http.service';
import { HttpRouterService } from '../../service/http-router.service';
import { StorageService } from '../../service/storage.service';
import { Router, ActivatedRoute } from '@angular/router';
import { CityData } from './city.data';
import _ from 'underscore';
declare var mui: any;

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.less']
})
export class HomeComponent implements OnInit {

  public step = '';

  public _cityList = [];
  public province = '';
  public isShow = false;
  public showProvince = '全国';

  public danList = [];
  public danProvince = '';
  public param = '';

  public createOrderParam = '';


  constructor(
    private _http: HttpService,
    private _router: Router,
    private _activatedRoute: ActivatedRoute,
    private _storage: StorageService,
    private _httpRouter: HttpRouterService
  ) { }

  ngOnInit() {
    this._activatedRoute.queryParams.subscribe(params => {
      this.param = params.param;
    });
    this._cityList = CityData;
    this.getDanList();
    mui.init({
      swipeBack: true //启用右滑关闭功能
    });
  }

  // 城市选择框
  showChoose() {
    this.isShow = true;
  }

  // 选择城市
  checkProvince(id= '', name= '全国') {
    this.province = id;
    this.showProvince = name;
    this.isShow = false;
    this.param = 'province=' + this.province;
    this.getDanList();
  }

  // 展示订单
  getDanList() {
    this._http.post(this._httpRouter.danList, this.param).then(res => {
      if (res.code === 1) {
        this.danList = res.data;
      }
    });
  }

  tips(danId, danMoney, $event) {
    $event.preventDefault();
    $event.stopPropagation();
    const btnArray = ['否', '是'];
    const that = this;
    mui.confirm('您确认要抢这个单子吗？', '抢单', btnArray, function(e) {
      if (e.index === 1) {
        that.createOrderParam = 'token=' + that._storage.getLocal('token') +
                                '&customer_id=' + danId +
                                '&order_money=' + danMoney;
        console.log(that.createOrderParam);
        that._http.post(that._httpRouter.createorder, that.createOrderParam).then( res => {
          if (res.code === 1) {
            that.getDanList();
          }
          alert(res.msg);
        });
      } else {
        return;
      }
    });
  }

}
